<template>
  <div>
    <p class="pinformation">样品统计信息</p>
    <el-form :model="form" ref="queryRef" label-width="110">
      <el-row>
        <el-form-item label="样品总数：" prop="fileMc">
          {{ form.sampleCodeCount }}
        </el-form-item>
        <el-form-item
          style="margin-left: 10px"
          label="现场质控样数量："
          prop="fileMc"
          label-width="120"
        >
          {{ form.qcSampleCount }}
        </el-form-item>
        <el-form-item style="margin-left: 10px" label="检测项目数量：" prop="fileMc">
          {{ form.jcxmCount }}
        </el-form-item>
        <el-form-item
          style="margin-left: 10px"
          label="点位数量："
          prop="fileMc"
          label-width="70"
        >
          {{ form.pointCount }}
        </el-form-item>
        <el-form-item
          style="margin-left: 10px"
          label="现场出数的样品数量："
          prop="fileMc"
          label-width="150"
        >
          {{ form.localWriteCount }}
        </el-form-item>
        <el-form-item
          style="margin-left: 10px"
          label="未交接数量："
          prop="fileMc"
          label-width="90"
        >
          {{ form.notWithCount }}
        </el-form-item>
        <el-form-item
          style="margin-left: 10px"
          label="已交接数量："
          prop="fileMc"
          label-width="90"
        >
          {{ form.withCount }}
        </el-form-item>
        <el-form-item style="margin-left: 10px" label="计划采样日期：" prop="fileMc">
          {{ form.cyDate }}
        </el-form-item>
      </el-row>
    </el-form>
    <p class="pinformation">搜索条件</p>
    <el-form ref="queryRef" label-width="80" :model="queryParams">
      <el-row>
        <el-col :span="6">
          <el-form-item label="检测类别：" prop="jclbId">
            <el-select
              v-model="queryParams.jclbId"
              style="width: 100%"
              filterable
              clearable
              @change="jclbChange"
            >
              <el-option
                v-for="item in jclbTypeList"
                :key="item.jclbId"
                :label="item.jclbName !== '' ? item.jclbName : item.jclbGroupName"
                :value="item.jclbId"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="点位名称：" prop="pointIds">
            <el-select
              v-model="queryParams.pointIds"
              style="width: 100%"
              filterable
              clearable
              multiple
              collapse-tags
              collapse-tags-tooltip
              :max-collapse-tags="1"
            >
              <el-option
                v-for="item in pointTable"
                :key="item.pointId"
                :label="item.pointName"
                :value="item.pointId"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="检测项目：" prop="jcxmName">
            <el-input clearable maxlength="100" v-model="queryParams.jcxmName" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="样品编号：" prop="sampleCode">
            <el-input clearable maxlength="100" v-model="queryParams.sampleCode" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="采样天数：" prop="sampleDay">
            <el-input clearable maxlength="100" v-model="queryParams.sampleDay" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="每天频次：" prop="sampleCount">
            <el-input clearable maxlength="100" v-model="queryParams.sampleCount" />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="状态：" prop="status" label-width="80">
            <el-select v-model="queryParams.status" placeholder="请选择状态">
              <el-option label="不限" value="" />
              <el-option label="现场出数" value="2" />
              <el-option label="未交接" value="0" />
              <el-option label="已交接" value="1" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-row style="justify-content: end">
            <el-button type="success" @click="handleQuery">搜索</el-button>
            <el-button class="resetBtn" @click="resetQuery">重置</el-button>
          </el-row>
        </el-col>
      </el-row>
    </el-form>
    <el-row>
      <p class="pinformation">样品详情表</p>
      <p class="totalStyle">共 {{ total }} 条数据</p>
    </el-row>
    <el-table border :data="tableData" ref="dataTableRef" v-loading="loading">
      <el-table-column
        align="center"
        show-overflow-tooltip
        label="检测类别"
        prop="jclbName"
        width="100"
      />
      <el-table-column
        align="center"
        show-overflow-tooltip
        label="点位名称"
        prop="pointName"
        width="100"
      />
      <el-table-column
        align="center"
        show-overflow-tooltip
        label="检测项目"
        prop="jcxmNames"
      >
        <template #default="scope">
          <span v-html="proxy.getSupAndSub(scope.row.jcxmNames)"></span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        show-overflow-tooltip
        label="样品编号"
        prop="sampleCode"
        width="170"
      />
      <el-table-column
        align="center"
        show-overflow-tooltip
        label="连测天数"
        prop="sampleDay"
        width="85"
      />
      <el-table-column
        align="center"
        show-overflow-tooltip
        label="每天次数"
        prop="sampleCount"
        width="85"
      />
      <el-table-column
        align="center"
        show-overflow-tooltip
        label="计划采样时间"
        prop="cyDate"
        width="120"
      />
      <el-table-column
        align="center"
        show-overflow-tooltip
        label="交接时间"
        prop="getDate"
        width="120"
      />
      <el-table-column
        align="center"
        show-overflow-tooltip
        label="是否分包"
        prop="isPackage"
        width="85"
      />
      <el-table-column align="center" show-overflow-tooltip label="当前状态" width="90">
        <template #default="scope">
          {{getStatus(scope.row)}}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        show-overflow-tooltip
        label="采样员"
        prop="cyUserName"
      />
      <el-table-column
        align="center"
        show-overflow-tooltip
        label="操作"
        width="85"
        fixed="right"
      >
        <template #default="scope">
          <el-button link @click="handleView(scope.row)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog
      :class="'el-dialog-theme3'"
      v-model="taskOpen"
      title="选择任务"
      width="30%"
      append-to-body
      @close="closedDialog"
      draggable
      :close-on-click-modal="false"
    >
      <el-select v-model="taskId" filterable clearable style="width: 100%">
        <el-option
          v-for="item in taskList"
          :value="item.taskId"
          :key="item.taskId"
          :label="item.title + '　' + item.tableName"
        ></el-option>
      </el-select>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="success" @click="openFormList">打 开</el-button>
          <el-button type="danger" @click="taskOpen = false">关 闭</el-button>
        </div>
      </template>
    </el-dialog>
    <el-dialog
      :class="'el-dialog-theme3'"
      v-model="show"
      title="查看原始记录表详情"
      fullscreen
    >
      <formList ref="formListRef" />
    </el-dialog>
  </div>
</template>

<script setup>
import {
  getCyrwdFrom,
  getCyrwdList,
  getTaskIds,
  getPointTable,
} from "@/api/taskProgressDetails/index";
import { projectListJclb } from "@/api/store/jclb";
import formList from "@/views/cyCheck/public/common/formList";
const { proxy } = getCurrentInstance();
const form = ref({});
const queryParams = ref({ pageNum: 1, pageSize: 10, status: "" });
const tableData = ref([]);
const total = ref(0);
const projectId = ref("");
const loading = ref(false);
const show = ref(false);
const heightParams = ref([{ tableName: "data", bottomHeight: 21 }]);

const jclbTypeList = ref([]);
const pointTable = ref([]);

function getForm(val) {
  projectId.value = val;
  queryParams.value.projectId = val;
  getCyrwdFrom(val).then((res) => {
    form.value = res.data;
  });
  projectListJclb({ projectId: val }).then((res) => {
    jclbTypeList.value = res.data;
  });
  getList();
}

function getList() {
  loading.value = true;
  getCyrwdList(queryParams.value).then((res) => {
    tableData.value = res.rows;
    total.value = res.total;
    loading.value = false;
  });
}

defineExpose({
  getForm,
});

function getTableHeight() {
  proxy.adaptiveHeight(proxy, heightParams.value);
}

onMounted(() => {
  nextTick(function () {
    getTableHeight();
    window.addEventListener("resize", getTableHeight);
  });
});

onUpdated(() => {
  nextTick(function () {
    getTableHeight();
    window.addEventListener("resize", getTableHeight);
  });
});
onBeforeUnmount(() => {
  window.removeEventListener("resize", getTableHeight);
});

watch(
  () => total.value,
  () => {
    if (total.value) {
      heightParams.value = [{ tableName: "data", bottomHeight: 73 }];
    } else {
      heightParams.value = [{ tableName: "data", bottomHeight: 21 }];
    }
    getTableHeight();
  }
);

const taskId = ref("");
const taskList = ref([]);
const taskOpen = ref(false);
const formListRef = ref(null);

function handleView(row) {
  getTaskIds(row.sFId).then((res) => {
    if (res.data.length == 0) {
      proxy.$modal.msgError("未找到对应的表！");
      return;
    }
    if (res.data.length > 1) {
      taskList.value = res.data;
      taskOpen.value = true;
    } else {
      show.value = true;
      taskId.value = res.data[0].taskId;
      nextTick(() => {
        formListRef.value.getInfo(taskId.value);
      });
    }
  });
}

function openFormList() {
  if (!taskId.value) {
    proxy.$modal.msgError("未选择要打开的任务！");
    return;
  }
  show.value = true;
  nextTick(() => {
    formListRef.value.getInfo(JSON.parse(JSON.stringify(taskId.value)));
    taskOpen.value = false;
  });
}

function closedDialog() {
  taskId.value = "";
  taskList.value = [];
}

function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

function jclbChange(val) {
  // delete queryParams.value.pointId
  if (val) {
    getPointTable({ jclbId: val, projectId: projectId.value }).then((res) => {
      pointTable.value = res.data;
    });
  }
  // else {
  //   pointTable.value = []
  // }
}

function getStatus(row){
  if(row.getUserId){
    return '已交接'
  }
  if(row.isWith == 1 || row.resultValue){
    return '现场出数';
  }
  return '待交接';
}
</script>

<style></style>
